package com.zhxt.system.service.impl;

import com.zhxt.common.config.RuoYiConfig;
import com.zhxt.common.core.text.Convert;
import com.zhxt.common.utils.PdfUtil;
import com.zhxt.common.utils.SecurityUtils;
import com.zhxt.system.domain.ZhxtFiles;
import com.zhxt.system.domain.ZhxtFilesLookRecord;
import com.zhxt.system.domain.ZhxtSourceInfo;
import com.zhxt.system.mapper.ZhxtFilesMapper;
import com.zhxt.system.mapper.ZhxtSourceInfoMapper;
import com.zhxt.system.service.IZhxtFilesService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ZhxtFilesServiceImpl implements IZhxtFilesService {

    @Autowired
    private ZhxtFilesMapper zhxtFilesMapper;

    @Autowired
    private ZhxtSourceInfoMapper zhxtSourceInfoMapper;
    /**
     * 查询管理制度列表
     */
    public List<ZhxtFiles> getListM(ZhxtFiles zhxtFiles){

        if(zhxtFiles.getSourcestr().equals("1")){
            zhxtFiles.setDeptid(SecurityUtils.getDeptId().toString());
        }
        List<ZhxtFiles> list = zhxtFilesMapper.getListM(zhxtFiles);
        List<ZhxtFiles> collect = getZhxtFiles(list);
        return collect.isEmpty()?new ArrayList<>():collect;
    }

    private static List<ZhxtFiles> getZhxtFiles(List<ZhxtFiles> list) {
        List<ZhxtFiles> collect = list.stream().map(zhxtFile -> {
            zhxtFile.setCtype(StringEscapeUtils.escapeHtml4(zhxtFile.getCtype()));
            zhxtFile.setFilenum(StringEscapeUtils.escapeHtml4(zhxtFile.getFilenum()));
            zhxtFile.setFilename(StringEscapeUtils.escapeHtml4(zhxtFile.getFilename()));
            zhxtFile.setFiledesc(StringEscapeUtils.escapeHtml4(zhxtFile.getFiledesc()));
            zhxtFile.setFiletype(StringEscapeUtils.escapeHtml4(zhxtFile.getFiletype()));
            zhxtFile.setImgurl(StringEscapeUtils.escapeHtml4(zhxtFile.getImgurl()));
            zhxtFile.setFileurl(StringEscapeUtils.escapeHtml4(zhxtFile.getFileurl()));
            zhxtFile.setFilestatus(StringEscapeUtils.escapeHtml4(zhxtFile.getFilestatus()));
            zhxtFile.setStatus(StringEscapeUtils.escapeHtml4(zhxtFile.getStatus()));
            zhxtFile.setDeptid(StringEscapeUtils.escapeHtml4(zhxtFile.getDeptid()));
            zhxtFile.setSourcestr(StringEscapeUtils.escapeHtml4(zhxtFile.getSourcestr()));
            return zhxtFile;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 查询详细信息
     */
    public List<ZhxtSourceInfo> getMInfo(String contnum){
        List<ZhxtSourceInfo> list = zhxtFilesMapper.getMInfo(contnum);
        List<ZhxtSourceInfo> collect = getZhxtSourceInfos(list);
        return collect.isEmpty()?new ArrayList<>():collect;
    }

    private static List<ZhxtSourceInfo> getZhxtSourceInfos(List<ZhxtSourceInfo> list) {
        List<ZhxtSourceInfo> collect = list.stream().map(zhxtSourceInfo -> {
            zhxtSourceInfo.setImgurl(StringEscapeUtils.escapeHtml4(zhxtSourceInfo.getImgurl()));
            zhxtSourceInfo.setNamedesc(StringEscapeUtils.escapeHtml4(zhxtSourceInfo.getNamedesc()));
            zhxtSourceInfo.setSourcenum(StringEscapeUtils.escapeHtml4(zhxtSourceInfo.getSourcenum()));
            zhxtSourceInfo.setCtype(StringEscapeUtils.escapeHtml4(zhxtSourceInfo.getCtype()));
            return zhxtSourceInfo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 查询附件详细信息，返回数组列表
     */
    public List<ZhxtSourceInfo> getFInfo(String contnum){
        List<ZhxtSourceInfo> fInfo = zhxtSourceInfoMapper.getFInfo(contnum);
        List<ZhxtSourceInfo> collect = getZhxtSourceInfos(fInfo);
        return collect.isEmpty()?new ArrayList<>():collect;
    }

    /**
     * 查询期刊列表
     */
    public List<ZhxtFiles> getListT(ZhxtFiles zhxtFiles){
        List<ZhxtFiles> listT = zhxtFilesMapper.getListT(zhxtFiles);
        List<ZhxtFiles> list = getZhxtFiles(listT);
        return list.isEmpty()?new ArrayList<>():list;
    }

    /**
     * 查询详细信息
     */
    public List<ZhxtSourceInfo> getTInfo(String contnum){
        List<ZhxtSourceInfo> tInfo = zhxtFilesMapper.getTInfo(contnum);
        List<ZhxtSourceInfo> zhxtSourceInfos = getZhxtSourceInfos(tInfo);
        return zhxtSourceInfos.isEmpty()?new ArrayList<>():zhxtSourceInfos;
    }

    /**
     * 添加管理制度
     */
    public int addMFiles(ZhxtFiles zhxtFiles){
        //存储文件
        zhxtFiles.setDeptid(SecurityUtils.getDeptId().toString()); //设置部门id
        int aNum=zhxtFilesMapper.addMFiles(zhxtFiles);
        if(aNum > 0){
            //处理源文件路径
            String tmpSource=zhxtFiles.getFileurl().replace("/profile/upload",RuoYiConfig.getUploadPath().toString());
            //System.out.println(tmpSource);
            String source=tmpSource;
            String desFileName=Convert.toStr(zhxtFiles.getContnum());
            String desFilePath= RuoYiConfig.getUploadPath()+"/M/"+desFileName+"/";
            String imageType="png";
            try {
                Pair<Boolean, Object> pair = PdfUtil.pdfToImage(source, desFilePath, desFileName, imageType);
                if(pair.getLeft()){
                    //转换成功
                    List<String> fileList=(List<String>)pair.getRight();
                    int iSort=1;
                    for (String fileString:fileList){
                        //存储文件
                        ZhxtSourceInfo zhxtSourceInfo=new ZhxtSourceInfo();
                        zhxtSourceInfo.setCtype("M");
                        zhxtSourceInfo.setSourcenum(desFileName);
                        zhxtSourceInfo.setSortnum(iSort);
                        //处理存储路径
                        zhxtSourceInfo.setImgurl(fileString.replace(RuoYiConfig.getUploadPath().toString(),"/profile/upload").replace("\\",""));
                        zhxtSourceInfoMapper.addFile(zhxtSourceInfo);
                        iSort++;
                    }
                    return iSort;
                }else{
                    //删除文件
                    this.delFile(Convert.toStr(zhxtFiles.getContnum()));
                    return 0;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 添加中原太保期刊
     */
    public int addTFiles(ZhxtFiles zhxtFiles){
        //存储文件
        zhxtFiles.setDeptid(SecurityUtils.getDeptId().toString()); //设置部门id
        int aNum=zhxtFilesMapper.addTFiles(zhxtFiles);
        if(aNum > 0){
            //处理源文件路径
            String tmpSource=zhxtFiles.getFileurl().replace("/profile/upload",RuoYiConfig.getUploadPath().toString());
            //System.out.println(tmpSource);
            String source=tmpSource;
            String desFileName=Convert.toStr(zhxtFiles.getContnum());
            String desFilePath= RuoYiConfig.getUploadPath()+"/T/"+desFileName+"/";
            String imageType="png";
            try {
                Pair<Boolean, Object> pair = PdfUtil.pdfToImage(source, desFilePath, desFileName, imageType);
                if(pair.getLeft()){
                    //转换成功
                    List<String> fileList=(List<String>)pair.getRight();
                    int iSort=1;
                    for (String fileString:fileList){
                        //存储文件
                        ZhxtSourceInfo zhxtSourceInfo=new ZhxtSourceInfo();
                        zhxtSourceInfo.setCtype("T");
                        zhxtSourceInfo.setSourcenum(desFileName);
                        zhxtSourceInfo.setSortnum(iSort);
                        //处理存储路径
                        zhxtSourceInfo.setImgurl(fileString.replace(RuoYiConfig.getUploadPath().toString(),"/profile/upload").replace("\\",""));
                        zhxtSourceInfoMapper.addFile(zhxtSourceInfo);
                        iSort++;
                    }
                    return iSort;
                }else{
                    //删除文件
                    this.delFile(Convert.toStr(zhxtFiles.getContnum()));
                    return 0;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 删除文件
     */
    public int delFile(String contnum){
        int delNum=zhxtSourceInfoMapper.delFiles(contnum);
        return zhxtFilesMapper.delFile(contnum);
    }

    /**
     * 添加管理制度附件
     */
    public int addMFilesf(ZhxtSourceInfo zhxtSourceInfo){
        int addNum=0;
        //判断是否只有一个附件
        /*
        if(zhxtSourceInfo.getImgurl().indexOf(",") >= 0){
            List<String> imgurlList = Arrays.asList(zhxtSourceInfo.getImgurl().split(","));
            for (String imgurl:imgurlList){
                zhxtSourceInfo.setImgurl(imgurl);
                addNum+=zhxtSourceInfoMapper.addFile(zhxtSourceInfo);
            }
        }else{
            addNum+=zhxtSourceInfoMapper.addFile(zhxtSourceInfo);
        }
        return  addNum;
         */
        return zhxtSourceInfoMapper.addFile(zhxtSourceInfo);
    }

    /**
     * 审批管理制度
     */
    public int spMFile(ZhxtFiles zhxtFiles){
        return zhxtFilesMapper.spMFile(zhxtFiles);
    }

    /**
     * 修改文件状态
     */
    public int updateFile(ZhxtFiles zhxtFiles){
        zhxtFiles.setDeptid(SecurityUtils.getDeptId().toString());
        return zhxtFilesMapper.updateFile(zhxtFiles);
    }

    /**
     * 删除附件
     */
    public int delFilef(ZhxtSourceInfo zhxtSourceInfo){
        return  zhxtSourceInfoMapper.delFilef(zhxtSourceInfo);
    }

    /**
     * 登记浏览次数及行为
     */
    public int openMFile(String contnum){
        //登记浏览行为
        ZhxtFilesLookRecord zhxtFilesLookRecord=new ZhxtFilesLookRecord();
        zhxtFilesLookRecord.setUserid(SecurityUtils.getUserId());
        zhxtFilesLookRecord.setUsername(SecurityUtils.getUsername());
        zhxtFilesLookRecord.setDeptno(SecurityUtils.getDeptId());
        zhxtFilesLookRecord.setContnum(contnum);
        zhxtFilesLookRecord.setOpentime(LocalDateTime.now());
        zhxtFilesMapper.openMFile(zhxtFilesLookRecord);
        int lookid=zhxtFilesLookRecord.getId();
        //浏览次数加1
        zhxtFilesMapper.addLookNums(contnum);
        return lookid;
    }
    /**
     * 关闭浏览
     */
    public int closeMFile(int lookid){
        ZhxtFilesLookRecord zhxtFilesLookRecord=new ZhxtFilesLookRecord();
        zhxtFilesLookRecord.setId(lookid);
        zhxtFilesLookRecord.setClosetime(LocalDateTime.now());
        int ccnum=zhxtFilesMapper.closeMFile(zhxtFilesLookRecord);
        return ccnum;
    }

    /**
     * 查询浏览流水
     */
    public List<ZhxtFilesLookRecord> getMLookList(String contnum){
        List<ZhxtFilesLookRecord> mLookList = zhxtFilesMapper.getMLookList(contnum);
        return mLookList.isEmpty()?new ArrayList<>():mLookList;
    }
}
